package com.chaofan.sell.handler;

import com.chaofan.sell.config.ProjectUrlConfig;
import com.chaofan.sell.exception.ResponseException;
import com.chaofan.sell.exception.SellException;
import com.chaofan.sell.exception.SellerAuthorizeException;
import com.chaofan.sell.utils.ResultVOUtil;
import com.chaofan.sell.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Fairy
 * @date 2021/11/3
 */
@ControllerAdvice
public class SellerExceptionHandler {

    @Autowired
    private ProjectUrlConfig urlConfig;

    /**
     * 拦截登陆异常
     */
    @ExceptionHandler(SellerAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView("redirect:".concat(urlConfig.getWechatOpenAuthorize())
                .concat("/sell/wechat/qrAuthorize"
                        .concat("?returnUrl=").concat(urlConfig.getSell()).concat("/sell/seller/login")));
    }

    @ExceptionHandler(SellException.class)
    @ResponseBody
    public ResultVO handlerSellException(SellException e) {
        return ResultVOUtil.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ResponseException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)//403
    public void handlerResponseException() {

    }
}
